import numpy as np
from sklearn.naive_bayes import GaussianNB

input_file = 'F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter02/data_multivar.txt'

X = []
y = []
with open(input_file,'r') as f:
    for line in f.readlines():
        data = [float(x) for x in line.split(',')]
        X.append(data[:-1])
        y.append(data[-1])

X = np.array(X)
y = np.array(y)

# 把数据集划分为训练集和测试集
from sklearn import model_selection
# test_size设为0.25，表示分配25%的数据给测试集
X_train,X_test,y_train,y_test = model_selection.train_test_split(X,y,random_state=5,test_size=0.25)
# 构建分类器
classifier_Gaussiannb = GaussianNB()
classifier_Gaussiannb.fit(X_train,y_train)

# 接下来计算分类器的准确率
print("准确度:{:.3f}".format(classifier_Gaussiannb.score(X_test,y_test)))